package com.example.wacoffee_manage.controller;

import com.example.wacoffee_manage.DTO.getLastDTO;
import com.example.wacoffee_manage.Service.reportService;
import com.example.wacoffee_manage.pojo.TopProducts;
import com.example.wacoffee_manage.pojo.TopProduct;
import com.example.wacoffee_manage.utils.Result;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/orderReport")
public class reportController {

    @Autowired
    private reportService reportService;
    @GetMapping("/getLastOrders")
    public Result getLastDaysOrders(@RequestParam LocalDate startDate, @RequestParam LocalDate endDate){
        getLastDTO getLastDTO = new getLastDTO(startDate,endDate);
        return Result.success(reportService.getLastDaysOrders(getLastDTO));
    }


    @GetMapping("/getLastUser")
    public Result getLastUser(@RequestParam LocalDate startDate, @RequestParam LocalDate endDate){
        getLastDTO getLastDTO = new getLastDTO(startDate,endDate);
        return Result.success(reportService.getLastUser(getLastDTO));
    }
    @GetMapping("/export")
    public void exportBusinessReport(HttpServletResponse response) {
        reportService.exportBusinessData(response);
    }

    @GetMapping("/getTopProducts")
    public Result getTop5Products(@RequestParam LocalDate startDate, @RequestParam LocalDate endDate){
        getLastDTO getLastDTO = new getLastDTO(startDate,endDate);
        int total= reportService.getTotalGoods(getLastDTO);
        List<TopProduct> topProducts = reportService.getTopProducts(getLastDTO);
        String[] names= new String[total];
        int[] sales= new int[total];
        for(int i=0;i<total;i++){
            if(i<topProducts.size()){
                names[i]=topProducts.get(i).getName();
                sales[i]=topProducts.get(i).getSales();
            }
            else{
                names[i]="无";
                sales[i]=0;
            }
        }
        return Result.success(new TopProducts(names,sales));
    }
}